import pandas as pd
import pytest
import pvlib
import requests
from tests.conftest import (
    RERUNS,
    RERUNS_DELAY,
    assert_frame_equal,
    assert_index_equal,
)


@pytest.fixture
def hourly_index():
    hourly_index = pd.date_range(start='2022-01-01 00:30+01:00', freq='60min',
                                 periods=24, name='dateTime')
    hourly_index.freq = None
    return hourly_index


@pytest.fixture
def hourly_index_start_utc():
    hourly_index_left_utc = pd.date_range(
        start='2023-01-01 00:00+00:00', freq='30min', periods=24*2,
        name='dateTime')
    hourly_index_left_utc.freq = None
    return hourly_index_left_utc


@pytest.fixture
def hourly_dataframe(hourly_index):
    ghi = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 5.0, 73.0, 152.0, 141.0, 105.0,
           62.0, 65.0, 62.0, 11.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    dni = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 30.0, 233.0, 301.0, 136.0, 32.0,
           0.0, 3.0, 77.0, 5.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
    return pd.DataFrame(data={'ghi': ghi, 'dni': dni}, index=hourly_index)


@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_solargis(hourly_dataframe):
    data, meta = pvlib.iotools.get_solargis(
        latitude=48.61259, longitude=20.827079,
        start='2022-01-01', end='2022-01-01',
        tz='GMT+01', variables=['GHI', 'DNI'],
        time_resolution='HOURLY', api_key='demo')
    assert_frame_equal(data, hourly_dataframe)


@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_solargis_utc_start_timestamp(hourly_index_start_utc):
    data, meta = pvlib.iotools.get_solargis(
        latitude=48.61259, longitude=20.827079,
        start='2023-01-01', end='2023-01-01',
        variables=['GTI'],
        timestamp_type='start',
        time_resolution='MIN_30',
        map_variables=False, api_key='demo')
    assert 'GTI' in data.columns  # assert that variables aren't mapped
    assert_index_equal(data.index, hourly_index_start_utc)


@pytest.mark.remote_data
@pytest.mark.flaky(reruns=RERUNS, reruns_delay=RERUNS_DELAY)
def test_get_solargis_http_error():
    # Test if HTTPError is raised if date outside range is specified
    match = r"request fromDate .* is before the available start date"
    with pytest.raises(requests.HTTPError, match=match):
        _, _ = pvlib.iotools.get_solargis(
            latitude=48.61259, longitude=20.827079,
            start='1920-01-01', end='1920-01-01',  # date outside range
            variables=['GHI', 'DNI'], time_resolution='HOURLY', api_key='demo')
